﻿
Option Explicit On
Imports CRM.BusinessLayer
Imports System.Data
Imports System.ComponentModel

Public Class frmAddCharge

    Private m_strCurrencyChar As String

    Public Enum Mode
        NotSet = 0
        Products = 1
        SLA = 2
        ActualCharge = 3
    End Enum

    Public Enum ChargeType
        NotSet = 0
        Charge = 1
        Credit = -1
    End Enum

    Public _Mode As Mode
    Public _ChargeType As ChargeType
    Private _ProductId As Integer
    Private _ProductName As String

    Public Property ProductId() As Integer
        Get
            Return _ProductId
        End Get
        Set(ByVal value As Integer)
            _ProductId = value
            _ProductName = Products.GetProductName(value)
        End Set
    End Property

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub AddCharge_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        Me.Title = "Add Charge in " & CommonDataFunctions.GetCurrencyCodeDesc(GlobalCRM.CurrentCaseID)
        m_strCurrencyChar = CommonDataFunctions.GetCurrencyCodeCharacter(GlobalCRM.CurrentCaseID)

        If _Mode = Mode.NotSet Or _ChargeType = ChargeType.NotSet Or _ProductId = 0 Then ' Or _ChargeTypeId = 0 Then
            CommonFuncs.DisplayMessage("Mode, Charge Type or Product Id not set!  Contact developer!", CommonDialog.CustomDialogIcons.Information, "Error")
            Me.Close()
            Exit Sub
        End If

        If Me._ChargeType = ChargeType.Charge Then

            With Me.drpType
                '.ItemsSource = Nothing
                .DisplayMemberPath = "ChargeType_Name"
                .SelectedValuePath = "ChargeType_Id"
                .ItemsSource = CType(Accounts.ChargeTypeList(GlobalCRM.CurrentCaseID), IListSource).GetList
                .SelectedIndex = -1
            End With
        ElseIf Me._ChargeType = ChargeType.Credit Then

            With Me.drpType
                '.ItemsSource = Nothing
                .DisplayMemberPath = "chargetype_name"
                .SelectedValuePath = "chargetype_categoryid"
                .ItemsSource = CType(Accounts.CreditTypeList, IListSource).GetList
                .SelectedIndex = -1
            End With
        End If
    End Sub

    Private Sub drpType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles drpType.SelectionChanged

        Me.txtNet.Text = "0.00"
        Me.txtTPCost.Text = "0.00"
        Me.txtIncome.Text = "0.00"

        If Me.drpType.SelectedIndex >= 0 Then

            If Me._ChargeType = ChargeType.Charge Then

                Me.txtVAT.Text = CType(Me.drpType.ItemsSource, System.Data.DataView).Table.Rows(Me.drpType.SelectedIndex).Item("chargetype_vat")
                Me.txtGross.Text = CType(Me.drpType.ItemsSource, System.Data.DataView).Table.Rows(Me.drpType.SelectedIndex).Item("ChargeType_Amount")

            ElseIf Me._ChargeType = ChargeType.Credit Then


                Me.txtVAT.Text = CType(Me.drpType.ItemsSource, System.Data.DataView).Table.Rows(Me.drpType.SelectedIndex).Item("chargetype_vat")
                Me.txtGross.Text = "0.00" 'CType(Me.drpType.DataSource, DataTable).Rows(Me.drpType.SelectedIndex).Item("ChargeType_Amount")

            End If

        Else
            Me.txtVAT.Text = "0.00"
            Me.txtGross.Text = "0.00"
        End If

    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        If Not IsNumeric(Me.txtNet.Text) Then
            CommonFuncs.DisplayMessage("Not a valid amount", CommonDialog.CustomDialogIcons.Information, "Error")
            Me.txtNet.Focus()
            Exit Sub
        End If

        If Not IsNumeric(Me.txtTPCost.Text) Then
            CommonFuncs.DisplayMessage("Not a valid TP Cost", CommonDialog.CustomDialogIcons.Information, "Error")
            Me.txtTPCost.Focus()
            Exit Sub
        End If

        If Me.drpType.SelectedIndex = -1 Then
            CommonFuncs.DisplayMessage("Please select a charge type", CommonDialog.CustomDialogIcons.Information, "Error")
            Me.drpType.Focus()
            Exit Sub
        End If

        If Me.txtNet.Text <= 0 Then
            If Me._ChargeType = ChargeType.Charge Then
                CommonFuncs.DisplayMessage("Charge must have a positive amount", CommonDialog.CustomDialogIcons.Information, "Error")
            Else
                CommonFuncs.DisplayMessage("Credit must be for a positive amount", CommonDialog.CustomDialogIcons.Information, "Error")
            End If
            Me.txtNet.Focus()
            Exit Sub
        End If

        If CDbl(Me.txtTPCost.Text) > CDbl(Me.txtNet.Text) Then
            'MsgBox(txtTPCost.Text.ToString & " > " & txtNet.Text.ToString)
            CommonFuncs.DisplayMessage("Third party cost cannot be higher than the charge amount", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If

        Dim singleObject As Singleton = Singleton.Instance

        Select Case _Mode

            Case Mode.Products
                'Adding a default charge in the product management screen

                If Products.CreateDefaultCharge(Me._ProductId, Me._ProductName, Me.drpType.Text, singleObject.CurrentConName, Me.txtVAT.Text, Me.txtNet.Text, Me.txtTPCost.Text, Me.drpType.SelectedValue, Me._ChargeType) > 0 Then
                    Me.Close()
                End If

            Case Mode.SLA

                If GlobalCRM.CurrentCompanyID > 0 Then

                    If Products.CreateDefaultCharge(Me._ProductId, Me._ProductName, Me.drpType.Text, singleObject.CurrentConName, Me.txtVAT.Text, Me.txtNet.Text, Me.txtTPCost.Text, Me.drpType.SelectedValue, Me._ChargeType, GlobalCRM.CurrentCompanyID) > 0 Then
                        Me.Close()
                    End If

                Else
                    Exit Sub
                End If

            Case Mode.ActualCharge

                If Accounts.CreateCharge(GlobalCRM.CurrentCaseID, GlobalCRM.CurrentCandidateID, Me.drpType.SelectedValue, Me.drpType.Text, singleObject.CurrentConName, GlobalCRM.CurrentCompanyID, Me.txtVAT.Text, Me.txtNet.Text, Me.txtTPCost.Text, Me._ChargeType, m_strCurrencyChar) > 0 Then
                    Me.Close()

                End If

        End Select

    End Sub



    Private Sub txtVAT_Validating(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles txtVAT.TextChanged

        If Not IsNumeric(Me.txtVAT.Text) Then
            CommonFuncs.DisplayMessage("Not a valid VAT figure", CommonDialog.CustomDialogIcons.Information, "Error")
            Me.txtVAT.Text = 0
            Me.txtVAT.Focus()
            'Exit Sub
        End If

        '  MsgBox("Net: " & txtNet.Text.ToString & " = Vat: " & txtVAT.Text.ToString & " x " & txtNet.Text.ToString & " / 100")
        If txtVAT.Text = "0" Then
            Exit Sub
        End If
        txtGross.Text = CDbl(txtNet.Text) + (CDbl(txtVAT.Text) * CDbl(txtNet.Text) / 100)
        txtIncome.Text = CDbl(txtNet.Text) - CDbl(txtTPCost.Text)

    End Sub

    Private Sub txtTPCost_Validating(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles txtTPCost.TextChanged

        If Not IsNumeric(Me.txtTPCost.Text) Then
            CommonFuncs.DisplayMessage("Not a valid Third Party Cost", CommonDialog.CustomDialogIcons.Information, "Error")
            Me.txtTPCost.Text = 0
            Me.txtTPCost.Focus()
            'Exit Sub
        End If

        '   MsgBox("Net: " & txtNet.Text.ToString & " = Vat: " & txtVAT.Text.ToString & " x " & txtNet.Text.ToString & " / 100")
        txtVAT.Text = Validate(txtVAT)
        txtGross.Text = txtNet.Text + (txtVAT.Text * txtNet.Text / 100)
        txtIncome.Text = txtNet.Text - txtTPCost.Text

    End Sub
    Private Function Validate(ByVal txt As TextBox) As Double
        If txt.Text.ToString = String.Empty Then
            Return 0
        End If
        If IsNumeric(txt.Text.ToString) Then
            Return txt.Text
        Else
            Return 0
        End If

    End Function
    Private Sub txtNet_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles txtNet.TextChanged
        If Not IsNumeric(Me.txtNet.Text) Then
            CommonFuncs.DisplayMessage("Not a valid amount", CommonDialog.CustomDialogIcons.Information, "Error")
            Me.txtNet.Text = 0
            Me.txtNet.Focus()
            'Exit Sub
        End If
        If txtVAT.Text = String.Empty Then
            txtGross.Text = txtNet.Text + (0 * txtNet.Text / 100)
        Else

            txtGross.Text = txtNet.Text + (txtVAT.Text * txtNet.Text / 100)
        End If
        If txtTPCost.Text = String.Empty Then
            txtIncome.Text = txtNet.Text - 0
        Else
            txtIncome.Text = txtNet.Text - txtTPCost.Text
        End If


    End Sub
End Class